package com.wlp.finance.biz.service.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.wlp.common.exception.BizException;
import com.wlp.common.exception.ParameterException;
import com.wlp.common.util.MyUtil;
import com.wlp.finance.biz.constant.RedisKeyConstant;
import com.wlp.finance.biz.domain.MemberBindPhone;
import com.wlp.finance.biz.dto.form.GetBase64CodeForm;
import com.wlp.finance.biz.dto.form.GetSmsCodeForm;
import com.wlp.finance.biz.dto.form.SmsCodeResult;
import com.wlp.finance.biz.enums.SmsCodeTypeEnum;
import com.wlp.finance.biz.service.MemberBindPhoneService;
import com.wlp.finance.biz.service.MemberLoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @author wlp
 * @date 2024/04/24 09:46
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class MemberLoginServiceImpl implements MemberLoginService {
    final RedisTemplate<String,Object> redisTemplate;
    final MemberBindPhoneService memberBindPhoneService;
    /**
     *获取客户端id
     * @Author: wlp
     * @Date: 2024/4/24 9:48
     * @return: java.lang.String
     **/
    @Override
    public String getClientId() {
        return UUID.randomUUID().toString().replace("-","");
    }

    @Override
    public String getBase64Code(GetBase64CodeForm form) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(300, 192, 5, 1000);
        String code=lineCaptcha.getCode();
        //将图形验证码写在redis中
        redisTemplate.opsForValue().set(RedisKeyConstant.GRAPHIC_VERIFICATION_CODE +form.getClientId(),code,
                15, TimeUnit.MINUTES);
        return lineCaptcha.getImageBase64();
    }
    
    /**
     * 
     * @Author: wlp
     * @Date: 2024/5/29 20:26
     * @param form: 
     * @return: void
     **/
    @Override
    public void sendSmsCode(GetSmsCodeForm form) {
        //校验图形验证码是否正确
        checkBase64Code(form.getClientId(), form.getCode());
        String key = RedisKeyConstant.SMS_CODE + form.getSmsCodeType() + form.getPhone();
        SmsCodeResult smsCodeResult = (SmsCodeResult) redisTemplate.opsForValue().get(key);
        if (smsCodeResult != null) {
            Duration duration = com.bage.common.util.DateUtil.getDuration(smsCodeResult.getGetTime(), com.bage.common.util.DateUtil.getSystemTime());
            if (duration.getSeconds() < 60) {
                throw new BizException("验证码获取太频繁，请稍后重试");
            }
        }
        MemberBindPhone memberBindPhone = memberBindPhoneService.getMemberByPhone(form.getPhone());
        if (form.getSmsCodeType().equals(SmsCodeTypeEnum.REG.getCode()) && memberBindPhone != null) {
            throw new ParameterException("phone", "该手机号已注册！");
        }
        if (form.getSmsCodeType().equals(SmsCodeTypeEnum.LOGIN.getCode()) && memberBindPhone == null) {
            throw new ParameterException("phone", "该手机号未注册！");
        }
        int smsCode = MyUtil.getRandom(6);
        smsCodeResult = new SmsCodeResult();
        smsCodeResult.setCode(String.valueOf(smsCode));
        smsCodeResult.setGetTime(com.bage.common.util.DateUtil.getSystemTime());
        redisTemplate.opsForValue().set(key, smsCodeResult, 15, TimeUnit.MINUTES);
        log.info("客户端id{},手机号：{},短信验证码：{}", form.getClientId(), form.getPhone(), smsCode);
//        smsService.sendSmsCode(form.getPhone(), smsCodeResult.getCode(), form.getSmsCodeType());
        //todo 调用第三方短信发送接口
    }

    /**
     * 
     * @Author: wlp
     * @Date: 2024/5/29 20:26
     * @param clientId: 
     * @param code: 
     * @return: boolean
     **/
    @Override
    public boolean checkBase64Code(String clientId,String code){
        //生成图片，获取base64编码字符串
        String cacheCode=(String)redisTemplate.opsForValue().get(RedisKeyConstant.GRAPHIC_VERIFICATION_CODE+clientId);
        //拿出比较后就要删除我们现在生成的短信验证码
        redisTemplate.delete(RedisKeyConstant.GRAPHIC_VERIFICATION_CODE+clientId);
        if(!code.equals(cacheCode)){
            throw new ParameterException("code","图形验证码错误！");
        }
        return true;
    }

}
